//
//  DailyCalendarView.m
//  Deputy
//
//  Created by Caesar on 30/10/2014.
//  Copyright (c) 2014 Caesar Li
//
#import "DailyCalendarView.h"
#import "NSDate+CL.h"
#import "UIColor+CL.h"
#import "Masonry.h"
#import "CLWeeklyCalendarView.h"
@interface DailyCalendarView()
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIView *dateLabelContainer;
@end


#define DATE_LABEL_SIZE 28
#define DATE_LABEL_FONT_SIZE 13
#define seleColor 0x8488ff
@implementation DailyCalendarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self addSubview:self.dateLabelContainer];
        
        UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dailyViewDidClick:)];
        [self addGestureRecognizer:singleFingerTap];
    }
    return self;
}
-(UIView *)dateLabelContainer
{
    if(!_dateLabelContainer){
        float x = (self.bounds.size.width - DATE_LABEL_SIZE)/2;
        _dateLabelContainer = [[UIView alloc] initWithFrame:CGRectMake(x-3, 0, DATE_LABEL_SIZE+5, DATE_LABEL_SIZE-11)];
        _dateLabelContainer.backgroundColor = [UIColor clearColor];
        _dateLabelContainer.layer.cornerRadius = 8;
        _dateLabelContainer.clipsToBounds = YES;
        [_dateLabelContainer addSubview:self.dateLabel];
    }
    return _dateLabelContainer;
}
-(UILabel *)dateLabel
{
    if(!_dateLabel){
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, -6, DATE_LABEL_SIZE, DATE_LABEL_SIZE)];
        _dateLabel.backgroundColor = [UIColor clearColor];
        _dateLabel.textColor = [UIColor whiteColor];
        _dateLabel.textAlignment = NSTextAlignmentCenter;
        _dateLabel.font = [UIFont systemFontOfSize:DATE_LABEL_FONT_SIZE];
    }
    
    return _dateLabel;
}

-(void)setDate:(NSDate *)date
{
    _date = date;
    
    [self setNeedsDisplay];
}
-(void)setBlnSelected: (BOOL)blnSelected
{
    _blnSelected = blnSelected;
    [self setNeedsDisplay];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    self.dateLabel.text = [self.date getDateOfMonth];
    
}

-(void)markSelected:(BOOL)blnSelected
{
    //    DLog(@"mark date selected %@ -- %d",self.date, blnSelected);
    if([self.date isDateToday]){
        self.dateLabelContainer.backgroundColor = (blnSelected)?[UIColor colorWithHex:0x8488ff]: [UIColor colorWithHex:0x5ca7ff];
        
        self.dateLabel.textColor = (blnSelected)?[UIColor whiteColor]:[UIColor whiteColor];
    }else{//0x0081c1
        
        self.dateLabelContainer.backgroundColor = (blnSelected)?[UIColor colorWithHex:seleColor]: [UIColor clearColor];//圆圈颜色
        
        //        self.dateLabel.textColor = (blnSelected)?[UIColor colorWithRed:52.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1.0]:
        self.dateLabel.textColor = (blnSelected)?[UIColor whiteColor]:
        [self colorByDate];
        
    }
    if (self.isPickDate == YES) {
        if (!_lineView) {
            _lineView = [CLWeeklyCalendarView shareLineView];
            //            _lineView.backgroundColor = [UIColor colorWithHex:0x5ca7ff];
        }
        [self addSubview:_lineView];
        //        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        //            make.top.equalTo(self.mas_bottom).offset(-2);
        //            make.left.equalTo(self.mas_left);
        //            make.right.equalTo(self.mas_right);
        //            make.height.equalTo(@(2));
        //        }];
        CGRect frame = self.frame;
        frame.origin.x = 2;
        frame.origin.y = self.frame.size.height-6;
        frame.size.height = 2;
        frame.size.width = frame.size.width;
        _lineView.frame = frame;
        [_lineView layoutIfNeeded];
        self.isPickDate = NO;

    }
    
    if (blnSelected) {
        NSLog(@"被打了！！！！！");
        if (!_lineView) {
            _lineView = [CLWeeklyCalendarView shareLineView];
//            _lineView.backgroundColor = [UIColor colorWithHex:0x5ca7ff];
        }
        
        [self addSubview:_lineView];
//        [_lineView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.mas_bottom).offset(-2);
//            make.left.equalTo(self.mas_left);
//            make.right.equalTo(self.mas_right);
//            make.height.equalTo(@(2));
//        }];
//        self.backgroundColor = [UIColor yellowColor];
        CGRect frame = self.frame;
        frame.origin.x = 2;
        frame.origin.y = self.frame.size.height-6;
        frame.size.height = 2;
        frame.size.width = frame.size.width-4;
        _lineView.frame = frame;
        [_lineView layoutIfNeeded];
//        if (!_lineView) {
//            NSLog(@"%@",_lineView.accessibilityValue);
//        }
    }
    _lineView.hidden = YES;
    
    
}
-(UIColor *)colorByDate
{//0x7BD1FF
    return [self.date isPastDate]?[UIColor colorWithHex:0xd1d1d1]:[UIColor colorWithHex:0x858585];
}

-(void)dailyViewDidClick: (UIGestureRecognizer *)tap
{
    [self.delegate dailyCalendarViewDidSelect: self.date];
}
@end



